package com.plugin.wxapp.rest;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mybatisplus.main.common.RedisUtil;
import com.mybatisplus.main.common.Result;
import com.plugin.wxapp.entity.MonsterModule;
import com.plugin.wxapp.entity.MonsterModuleImg;
import com.plugin.wxapp.service.MonsterModuleImgService;
import com.plugin.wxapp.service.MonsterModuleService;
import com.plugin.wxapp.service.MonsterUserService;
import com.plugin.wxapp.util.CommonConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("monster")
@Api(description = "相片合成接口")
public class MonsterController {

    @Autowired
    private MonsterModuleService monsterModuleService;

    @Autowired
    private MonsterModuleImgService monsterModuleImgService;

    @Autowired
    private MonsterUserService monsterUserService;

    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "图片信息查询", notes = "图片信息查询", produces = "application/json")
    @GetMapping(value = "/getMonsterInfo")
    public Result<JSONObject> getMonsterInfo() {
        Result<JSONObject> result = new Result<>();
        try {
            JSONObject jsonObject = new JSONObject();

            String monsterRedis = (String)redisUtil.get("monsters");
            String frameRedis = (String) redisUtil.get("frames");
            if(monsterRedis != null && frameRedis != null) {
                jsonObject.put("frames", JSONObject.parse(frameRedis));
                jsonObject.put("monsters", JSONObject.parse(monsterRedis));
                result.setResult(jsonObject);
                result.success("查询成功");
                return result;
            }

            List<MonsterModule> monsters = getImgInfo(CommonConstant.monster_group);
            List<MonsterModule> frames = getImgInfo(CommonConstant.frame_group);
            jsonObject.put("frames", frames);
            jsonObject.put("monsters", monsters);
            result.setResult(jsonObject);
            result.success("查询成功");

            redisUtil.set("monsters", JSONObject.toJSONString(monsters));
            redisUtil.set("frames", JSONObject.toJSONString(frames));
        } catch (Exception e) {
            e.printStackTrace();
            result.error500(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "添加模块数据", notes = "添加模块数据", produces = "application/json")
    @PostMapping(value = "/addMonsterModule")
    public Result<MonsterModule> addMonsterModule(@RequestBody MonsterModule monsterModule) {
        Result<MonsterModule> result = new Result<MonsterModule>();
        try {
            monsterModuleService.save(monsterModule);
            result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }

    @ApiOperation(value = "添加模块图片数据", notes = "添加模块图片数据", produces = "application/json")
    @PostMapping(value = "/addMonsterModuleImg")
    public Result<MonsterModuleImg> addMonsterModuleImg(@RequestBody MonsterModuleImg monsterModuleImg) {
        Result<MonsterModuleImg> result = new Result<MonsterModuleImg>();
        try {
            monsterModuleImgService.save(monsterModuleImg);
            result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }

    public List<MonsterModule> getImgInfo(String group) {
        QueryWrapper<MonsterModule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groupNum", group);
        queryWrapper.orderByAsc("sort");
        List<MonsterModule> monsters = monsterModuleService.list(queryWrapper);
        if (monsters != null && monsters.size() > 0) {
            for (MonsterModule monsterModule : monsters) {
                QueryWrapper<MonsterModuleImg> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("moduleId", monsterModule.getId());
                queryWrapper1.eq("status", "1");
                queryWrapper1.orderByAsc("sort");
                List<MonsterModuleImg> list = monsterModuleImgService.list(queryWrapper1);
                monsterModule.setImgList(list);
            }
        }
        return monsters;
    }

    @ApiOperation(value = "clear", notes = "clear", produces = "application/json")
    @PostMapping(value = "/clearRedis")
    public Result clearRedis() {
        Result result = new Result<>();
        try {
            redisUtil.del("monsters");
            redisUtil.del("frames");
            redisUtil.del("monster");
            result.success("清除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("操作失败");
        }
        return result;
    }

}
